<?php

/* 评价标签 */
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class EvaluateController extends AdminBaseController
{
    var $type=[
        '1'=>'好评',
        '0'=>'差评',
    ];
    public function index()
    {
        
        $list = Db::name('evaluate')
            ->order("list_order asc")
            ->paginate(20);
        
        $page = $list->render();
        $this->assign("page", $page);
            
        $this->assign('list', $list);
        $this->assign('type', $this->type);

        return $this->fetch();
    }


    public function add()
    {
        $this->assign('type', $this->type);
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();
            
            $name=trim($data['name']);
            
            if($name == ''){
                $this->error('请填写名称');
            }else{
                $check = Db::name('evaluate')->where("name='{$name}'")->find();
                if($check){
                    $this->error('同名标签已存在');
                }
            }

            $data['name']=$name;

            $type=$data['type'];
            if($type==0){
                $data['colour']='#646464';
            }
            $colour=$data['colour'];
            if($colour==''){
                $this->error('请选择颜色');
            }

            $id = Db::name('evaluate')->insertGetId($data);
            if(!$id){
                $this->error("添加失败！");
            }
            $this->resetcache();
            $this->success("添加成功！");
        }
    }

    public function edit()
    {
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('evaluate')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        
        $this->assign('type', $this->type);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();
            
            $id=$data['id'];
            $name=trim($data['name']);
            if($name == ''){
                $this->error('请填写名称');
            }else{
                $check = Db::name('evaluate')->where("name='{$name}' and id !='{$id}'")->find();
                if($check){
                    $this->error('同名标签已存在');
                }
            }

            $data['name']=$name;
            
            $type=$data['type'];
            if($type==0){
                $data['colour']='#646464';
            }
            $colour=$data['colour'];

            if($colour==''){
                $this->error('请选择颜色');
            }

            $rs = Db::name('evaluate')->update($data);

            if($rs === false){
                $this->error("保存失败！");
            }
            $this->resetcache();
            $this->success("保存成功！");
        }
    }
    
    public function listOrder()
    {
        $model = Db::name('evaluate');
        parent::listOrders($model);
        //$this->resetcache();
        $this->success("排序更新成功！");
    }

    public function del()
    {
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = Db::name('evaluate')->where("id={$id}")->delete();
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->resetcache();
        $this->success("删除成功！",url("evaluate/index"));
    }


    protected function resetcache(){

        $key='evaluatelist';

        $level=Db::name('evaluate')
                ->field('id,name,name_en,colour,type')
                ->order("list_order asc")
                ->select();
        if($level){
            setcaches($key,$level);
        }
    }
}